<template>
  <!-- 只能有一个根标签 -->
  <!-- 用了一个行列布局 -->
  <el-row
    type="flex"
    justify="space-between"
    align="middle"
    style="height: 40px; width: 100%"
  >
    <el-col>
      <span>{{ treeNode.name }}</span>
    </el-col>
    <el-col :span="4">
      <el-row type="flex" justify="end">
        <!-- 两个内容 -->
        <el-col>{{ treeNode.manager }}</el-col>
        <el-col>
          <!-- 下拉菜单 element -->
          <el-dropdown @command="handleCommand">
            <span> 操作<i class="el-icon-arrow-down" /> </span>
            <!-- 下拉菜单 -->
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="add">添加子部门</el-dropdown-item>
              <el-dropdown-item
                v-if="treeNode.manager != '负责人'"
                command="edit"
              >编辑子部门</el-dropdown-item>
              <el-dropdown-item
                v-show="treeNode.manager != '负责人'"
                command="del"
              >删除子部门</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-col>
      </el-row>
    </el-col>
  </el-row>
</template>

<script>
import { delDepartments, getDepartDetail } from '@/api/departments'
import { mapMutations } from 'vuex'
export default {
  name: '',
  components: {},
  props: {
    treeNode: {
      type: Object,
      required: true
    }
  },
  data() {
    return {}
  },
  computed: {},
  watch: {},
  created() { },
  mounted() { },
  methods: {
    ...mapMutations('departments', ['SET_showDialog', 'SET_NODE', 'SET_FORM_DATA']),
    async handleCommand(str) {
      // 通过str预定义字符串判定点的是哪个下拉框
      let res = null
      switch (str) {
        case 'add':
          // 新增
          // 把显示弹框的变量存到vuex
          this.SET_showDialog(true)
          // 把当前点击的部门信息存到vuex
          this.SET_NODE(this.treeNode)
          break
        case 'edit':
          // 编辑
          // 把显示弹框的变量存到vuex
          this.SET_showDialog(true)
          // 把当前点击的部门信息存到vuex
          this.SET_NODE(this.treeNode)
          // 发请求拿到部门信息
          res = await getDepartDetail(this.treeNode.id)
          this.SET_FORM_DATA(res)
          // 怎么把部门信息数据给add-dept组件渲染
          break
        case 'del':
          // 删除
          // ①弹框 Promise写的
          await this.$confirm('是否确认删除?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          })
          // ②发请求
          await delDepartments(this.treeNode.id)
          // ③触发父组件自定义时间, 重新拿数据
          this.$emit('delDepartments')
          // ④弹框提示删除成功
          this.$message.success('删除成功!')
          break
      }
    }
  }
}
</script>

<style scoped>
</style>
